package generator;

import java.text.MessageFormat;

public class SetterGetterBuilder extends SourceBuilder {
	protected StringBuilder mField = new StringBuilder();
	protected StringBuilder mMethod = new StringBuilder();

	@Override
	public void addMember(String visiblity, String type, String name, String description) {
		// field
		if ("public".equals(visiblity)) {
			mField.append(MessageFormat.format("    /** {0} */", description));
		} else {
			mField.append(MessageFormat.format("    // {0}", description));
		}
		mField.append("\r\n");
		mField.append(MessageFormat.format("    {0} {1} {2};", visiblity, type, getFieldName(name)));
		mField.append("\r\n");

		// getter
		String get = "    /**"
				+ "\r\n     * {0}を{1}する。"
				+ "\r\n     * "
				+ "\r\n     * @return {0}"
				+ "\r\n     */"
				+ "\r\n    public {2} {5}{3}() {6}"
				+ "\r\n        return {4};"
				+ "\r\n    {7}";
		String text;
		String method;
		if ("boolean".equals(type.toLowerCase())) {
			text = "判定";
			method = "is";
		} else {
			text = "取得";
			method = "get";
		}
		mMethod.append("\r\n");
		mMethod.append(MessageFormat.format(get, description, text, type, getFirstUppperCase(name), getFieldName(name), method, "{", "}"));
		mMethod.append("\r\n");
		mMethod.append("\r\n");

		// setter
		String set = "    /**"
				+ "\r\n     * {0}を設定する。"
				+ "\r\n     * "
				+ "\r\n     * @param {1} {0}"
				+ "\r\n     */"
				+ "\r\n    public void set{2}({3} {4}) {6}"
				+ "\r\n        {5} = {4};"
				+ "\r\n    {7}";
		mMethod.append(MessageFormat.format(set, description, name, getFirstUppperCase(name), type, name, getFieldName(name), "{", "}"));
		mMethod.append("\r\n");
	}

	@Override
	public String build() {
		StringBuilder source = new StringBuilder();
		if (getPkg() != null) {
			source.append("package ").append(getPkg()).append(";");
		}
		source.append("\r\n");
		source.append("\r\n");
		source.append("\r\n").append("/** {0} */");
		source.append("\r\n").append("public class {1} {3}");
		source.append("\r\n").append("{2}");
		source.append("\r\n").append("{4}");
		return MessageFormat.format(source.toString(), getDescription(), getName(), mField.toString() + mMethod.toString(), "{", "}");
	}

}
